<template>
  <el-main class="bg edit_wrap comtable_e">
    <el-form
      ref="form"
      :model="form"
      status-icon
      label-width="120px"
      v-if="is_view()"
    >
      <el-row class="row_ce">
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'regular_users') ||
              $check_field('add', 'regular_users') ||
              $check_field('set', 'regular_users')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="普通用户" prop="regular_users">
            <div v-if="user_group !== '管理员'">
              {{ get_user_session_regular_users(form['regular_users']) }}
              <el-select
                v-if="
                  user_group === '管理员' ||
                    (form['restaurant_orders_id'] &&
                      $check_field('set', 'regular_users')) ||
                    (!form['restaurant_orders_id'] &&
                      $check_field('add', 'regular_users'))
                "
                id="regular_users"
                v-model="form['regular_users']"
                :disabled="disabledObj['regular_users_isDisabled']"
              >
                <el-option
                  v-for="o in list_user_regular_users"
                  :key="o['username']"
                  :label="o['nickname'] + '-' + o['username']"
                  :value="o['user_id']"
                >
                </el-option>
              </el-select>
              <el-select
                v-else-if="$check_field('get', 'regular_users')"
                id="regular_users"
                v-model="form['regular_users']"
                :disabled="true"
              >
                <el-option
                  v-for="o in list_user_regular_users"
                  :key="o['username']"
                  :label="o['nickname'] + '-' + o['username']"
                  :value="o['user_id']"
                >
                </el-option>
              </el-select>
            </div>
            <el-select
              v-else
              id="regular_users"
              v-model="form['regular_users']"
              :disabled="disabledObj['regular_users_isDisabled']"
            >
              <el-option
                v-for="o in list_user_regular_users"
                :key="o['username']"
                :label="o['nickname'] + '-' + o['username']"
                :value="o['user_id']"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'user_name') ||
              $check_field('add', 'user_name') ||
              $check_field('set', 'user_name')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="用户姓名" prop="user_name">
            <el-input
              id="user_name"
              v-model="form['user_name']"
              placeholder="请输入用户姓名"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'user_name')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'user_name'))
              "
              :disabled="disabledObj['user_name_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'user_name')">
              {{ form['user_name'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'mobile_phone_number') ||
              $check_field('add', 'mobile_phone_number') ||
              $check_field('set', 'mobile_phone_number')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="手机号码" prop="mobile_phone_number">
            <el-input
              id="mobile_phone_number"
              v-model="form['mobile_phone_number']"
              placeholder="请输入手机号码"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'mobile_phone_number')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'mobile_phone_number'))
              "
              :disabled="disabledObj['mobile_phone_number_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'mobile_phone_number')">
              {{ form['mobile_phone_number'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'merchant_users') ||
              $check_field('add', 'merchant_users') ||
              $check_field('set', 'merchant_users')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="商家用户" prop="merchant_users">
            <el-select
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'merchant_users')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'merchant_users'))
              "
              id="merchant_users"
              v-model="form['merchant_users']"
              :disabled="disabledObj['merchant_users_isDisabled']"
            >
              <el-option
                v-for="o in list_user_merchant_users"
                :key="o['username']"
                :label="o['nickname'] + '-' + o['username']"
                :value="o['user_id']"
              >
              </el-option>
            </el-select>
            <el-select
              v-else-if="$check_field('get', 'merchant_users')"
              id="merchant_users"
              v-model="form['merchant_users']"
              :disabled="true"
            >
              <el-option
                v-for="o in list_user_merchant_users"
                :key="o['username']"
                :label="o['nickname'] + '-' + o['username']"
                :value="o['user_id']"
              >
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'merchant_name') ||
              $check_field('add', 'merchant_name') ||
              $check_field('set', 'merchant_name')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="商家名称" prop="merchant_name">
            <el-input
              id="merchant_name"
              v-model="form['merchant_name']"
              placeholder="请输入商家名称"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'merchant_name')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'merchant_name'))
              "
              :disabled="disabledObj['merchant_name_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'merchant_name')">
              {{ form['merchant_name'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'restaurant_name') ||
              $check_field('add', 'restaurant_name') ||
              $check_field('set', 'restaurant_name')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="餐厅名称" prop="restaurant_name">
            <el-input
              id="restaurant_name"
              v-model="form['restaurant_name']"
              placeholder="请输入餐厅名称"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'restaurant_name')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'restaurant_name'))
              "
              :disabled="disabledObj['restaurant_name_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'restaurant_name')">
              {{ form['restaurant_name'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'restaurant_id') ||
              $check_field('add', 'restaurant_id') ||
              $check_field('set', 'restaurant_id')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="餐厅编号" prop="restaurant_id">
            <el-input
              id="restaurant_id"
              v-model="form['restaurant_id']"
              placeholder="请输入餐厅编号"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'restaurant_id')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'restaurant_id'))
              "
              :disabled="disabledObj['restaurant_id_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'restaurant_id')">
              {{ form['restaurant_id'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'restaurant_phone_number') ||
              $check_field('add', 'restaurant_phone_number') ||
              $check_field('set', 'restaurant_phone_number')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="餐厅电话" prop="restaurant_phone_number">
            <el-input
              id="restaurant_phone_number"
              v-model="form['restaurant_phone_number']"
              placeholder="请输入餐厅电话"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'restaurant_phone_number')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'restaurant_phone_number'))
              "
              :disabled="disabledObj['restaurant_phone_number_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'restaurant_phone_number')">
              {{ form['restaurant_phone_number'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'restaurant_address') ||
              $check_field('add', 'restaurant_address') ||
              $check_field('set', 'restaurant_address')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="餐厅地址" prop="restaurant_address">
            <el-input
              id="restaurant_address"
              v-model="form['restaurant_address']"
              placeholder="请输入餐厅地址"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'restaurant_address')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'restaurant_address'))
              "
              :disabled="disabledObj['restaurant_address_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'restaurant_address')">
              {{ form['restaurant_address'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'dinner_time') ||
              $check_field('add', 'dinner_time') ||
              $check_field('set', 'dinner_time')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="用餐时间" prop="dinner_time">
            <el-date-picker
              :disabled="disabledObj['dinner_time_isDisabled']"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'dinner_time')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'dinner_time'))
              "
              id="dinner_time"
              v-model="form['dinner_time']"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
            <div v-else-if="$check_field('get', 'dinner_time')">
              {{ form['dinner_time'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'booking_amount') ||
              $check_field('add', 'booking_amount') ||
              $check_field('set', 'booking_amount')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="预定金额" prop="booking_amount">
            <el-input
              id="booking_amount"
              v-model="form['booking_amount']"
              placeholder="请输入预定金额"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'booking_amount')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'booking_amount'))
              "
              :disabled="disabledObj['booking_amount_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'booking_amount')">
              {{ form['booking_amount'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'order_balance') ||
              $check_field('add', 'order_balance') ||
              $check_field('set', 'order_balance')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="订单尾款" prop="order_balance">
            <el-input
              id="order_balance"
              v-model="form['order_balance']"
              placeholder="请输入订单尾款"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'order_balance')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'order_balance'))
              "
              :disabled="disabledObj['order_balance_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'order_balance')">
              {{ form['order_balance'] }}
            </div>
          </el-form-item>
        </el-col>
        <el-col
          v-if="
            user_group === '管理员' ||
              $check_field('get', 'booking_remarks') ||
              $check_field('add', 'booking_remarks') ||
              $check_field('set', 'booking_remarks')
          "
          :xs="24"
          :sm="12"
          :lg="8"
          class="el_form_item_warp"
        >
          <el-form-item label="预订备注" prop="booking_remarks">
            <el-input
              id="booking_remarks"
              v-model="form['booking_remarks']"
              placeholder="请输入预订备注"
              v-if="
                user_group === '管理员' ||
                  (form['restaurant_orders_id'] &&
                    $check_field('set', 'booking_remarks')) ||
                  (!form['restaurant_orders_id'] &&
                    $check_field('add', 'booking_remarks'))
              "
              :disabled="disabledObj['booking_remarks_isDisabled']"
            ></el-input>
            <div v-else-if="$check_field('get', 'booking_remarks')">
              {{ form['booking_remarks'] }}
            </div>
          </el-form-item>
        </el-col>
      </el-row>
      <el-col :xs="24" :sm="12" :lg="8" class="el_form_btn_warp">
        <el-form-item
          v-if="
            $check_action('/restaurant_orders/view', 'set') ||
              $check_action('/restaurant_orders/view', 'add') ||
              $check_option('/restaurant_orders/table', 'examine')
          "
        >
          <el-button type="primary" @click="submit()">提交</el-button>
          <el-button @click="cancel()">取消</el-button>
        </el-form-item>
        <el-form-item v-else>
          <el-button @click="cancel()">返回</el-button>
        </el-form-item>
      </el-col>
    </el-form>
  </el-main>
</template>

<script>
import mixin from '@/mixins/page.js'

export default {
  mixins: [mixin],
  data() {
    return {
      field: 'restaurant_orders_id',
      url_add: '~/api/restaurant_orders/add?',
      url_set: '~/api/restaurant_orders/set?',
      url_get_obj: '~/api/restaurant_orders/get_obj?',
      url_upload: '~/api/restaurant_orders/upload?',

      query: {
        restaurant_orders_id: 0,
      },

      form: {
        regular_users: 0, // 普通用户
        user_name: '', // 用户姓名
        mobile_phone_number: '', // 手机号码
        merchant_users: 0, // 商家用户
        merchant_name: '', // 商家名称
        restaurant_name: '', // 餐厅名称
        restaurant_id: '', // 餐厅编号
        restaurant_phone_number: '', // 餐厅电话
        restaurant_address: '', // 餐厅地址
        dinner_time: '', // 用餐时间
        booking_amount: '', // 预定金额
        order_balance: '', // 订单尾款
        booking_remarks: '', // 预订备注
        restaurant_orders_id: 0, // ID
      },
      disabledObj: {
        regular_users_isDisabled: false,
        user_name_isDisabled: false,
        mobile_phone_number_isDisabled: false,
        merchant_users_isDisabled: false,
        merchant_name_isDisabled: false,
        restaurant_name_isDisabled: false,
        restaurant_id_isDisabled: false,
        restaurant_phone_number_isDisabled: false,
        restaurant_address_isDisabled: false,
        dinner_time_isDisabled: false,
        booking_amount_isDisabled: false,
        order_balance_isDisabled: false,
        booking_remarks_isDisabled: false,
      },

      // 用户列表
      list_user_regular_users: [],
      // 用户组
      group_user_regular_users: '',

      // 用户列表
      list_user_merchant_users: [],
    }
  },
  methods: {
    /**
     * 获取普通用户用户列表
     */
    async get_list_user_regular_users() {
      // if(this.user_group !== "管理员" && this.form["regular_users"] === 0) {
      //     this.form["regular_users"] = this.user.user_id;
      // }
      var json = await this.$get('~/api/user/get_list?user_group=普通用户')
      if (json.result && json.result.list) {
        this.list_user_regular_users = json.result.list
      } else if (json.error) {
        console.error(json.error)
      }
    },
    /**
     * 获取普通用户用户组
     */
    async get_group_user_regular_users() {
      this.form['regular_users'] = this.user.user_id
      var json = await this.$get('~/api/user_group/get_obj?name=普通用户')
      if (json.result && json.result.obj) {
        this.group_user_regular_users = json.result.obj
      } else if (json.error) {
        console.error(json.error)
      }
    },
    get_user_session_regular_users(id) {
      var _this = this
      var user_id = { user_id: id }
      var url =
        '~/api/' + _this.group_user_regular_users.source_table + '/get_obj?'
      this.$get(url, user_id, function(res) {
        if (res.result && res.result.obj) {
          var arr = []
          for (let key in res.result.obj) {
            arr.push(key)
          }
          var arrForm = []
          for (let key in _this.form) {
            arrForm.push(key)
          }
          _this.form['regular_users'] = id
          _this.disabledObj['regular_users' + '_isDisabled'] = true
          for (var i = 0; i < arr.length; i++) {
            if (arr[i] !== 'examine_state' && arr[i] !== 'examine_reply') {
              for (var j = 0; j < arrForm.length; j++) {
                if (arr[i] === arrForm[j]) {
                  if (arr[i] !== 'regular_users') {
                    _this.form[arrForm[j]] = res.result.obj[arr[i]]
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                    break
                  } else {
                    _this.disabledObj[arrForm[j] + '_isDisabled'] = true
                  }
                }
              }
            }
          }
        }
      })
    },
    get_user_regular_users(id) {
      var obj = this.list_user_regular_users.getObj({ user_id: id })
      var ret = ''
      if (obj) {
        if (obj.nickname) {
          ret = obj.nickname
        } else {
          ret = obj.username
        }
      }
      return ret
    },

    /**
     * 获取商家用户用户列表
     */
    async get_list_user_merchant_users() {
      // if(this.user_group !== "管理员" && this.form["merchant_users"] === 0) {
      //     this.form["merchant_users"] = this.user.user_id;
      // }
      var json = await this.$get('~/api/user/get_list?user_group=商家用户')
      if (json.result && json.result.list) {
        this.list_user_merchant_users = json.result.list
      } else if (json.error) {
        console.error(json.error)
      }
    },
    get_user_merchant_users(id) {
      var obj = this.list_user_merchant_users.getObj({ user_id: id })
      var ret = ''
      if (obj) {
        if (obj.nickname) {
          ret = obj.nickname
        } else {
          ret = obj.username
        }
      }
      return ret
    },

    /**
     * 获取对象之前
     * @param {Object} param
     */
    get_obj_before(param) {
      var form = ''
      // 获取缓存数据附加
      form = $.db.get('form')
      $.push(this.form, form)

      if (this.form && form) {
        Object.keys(this.form).forEach(key => {
          Object.keys(form).forEach(dbKey => {
            // if(dbKey === "charging_standard"){
            // 	this.form['charging_rules'] = form[dbKey];
            // 	this.disabledObj['charging_rules_isDisabled'] = true;
            // };
            if (key === dbKey) {
              this.disabledObj[key + '_isDisabled'] = true
            }
          })
        })
      }
      if (
        this.form['dinner_time'] &&
        this.form['dinner_time'].indexOf('-') === -1
      ) {
        this.form['dinner_time'] = this.$toTime(
          parseInt(this.form['dinner_time']),
          'yyyy-MM-dd hh:mm:ss'
        )
      }
      $.db.del('form')

      return param
    },

    /**
     * 获取对象之后
     * @param {Object} json
     * @param {Object} func
     */
    get_obj_after(json, func) {
      if (this.form['dinner_time'] == '0000-00-00 00:00:00') {
        this.form['dinner_time'] = null
      }
      if (parseInt(this.form['dinner_time']) > 9999) {
        this.form['dinner_time'] = this.$toTime(
          parseInt(this.form['dinner_time']),
          'yyyy-MM-dd hh:mm:ss'
        )
      }
    },

    /**
     * 提交前验证事件
     * @param {Object} 请求参数
     * @return {String} 验证成功返回null, 失败返回错误提示
     */
    submit_check(param) {
      if (!param.dinner_time) {
        return '用餐时间不能为空'
      }
      return null
    },

    is_view() {
      var bl = this.user_group == '管理员'

      if (!bl) {
        bl = this.$check_action('/restaurant_orders/table', 'add')
        console.log(
          bl ? '你有表格添加权限视作有添加权限' : '你没有表格添加权限'
        )
      }
      if (!bl) {
        bl = this.$check_action('/restaurant_orders/table', 'set')
        console.log(
          bl ? '你有表格添加权限视作有修改权限' : '你没有表格修改权限'
        )
      }
      if (!bl) {
        bl = this.$check_action('/restaurant_orders/view', 'add')
        console.log(
          bl ? '你有视图添加权限视作有添加权限' : '你没有视图添加权限'
        )
      }
      if (!bl) {
        bl = this.$check_action('/restaurant_orders/view', 'set')
        console.log(
          bl ? '你有视图修改权限视作有修改权限' : '你没有视图修改权限'
        )
      }
      if (!bl) {
        bl = this.$check_action('/restaurant_orders/view', 'get')
        console.log(
          bl ? '你有视图查询权限视作有查询权限' : '你没有视图查询权限'
        )
      }

      console.log(
        bl
          ? '具有当前页面的查看权，请注意这不代表你有字段的查看权'
          : '无权查看当前页，请注意即便有字段查询权限没有页面查询权限也不行'
      )

      return bl
    },
    /**
     * 上传文件
     * @param {Object} param
     */
    uploadimg(param) {
      this.uploadFile(param.file, 'avatar')
    },
  },
  created() {
    this.get_list_user_regular_users()
    this.get_group_user_regular_users()
    this.get_list_user_merchant_users()
  },
}
</script>

<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
